﻿using System;
using System.Windows;
using System.Windows.Input;
using MDCAdminTool.Controls;

namespace MDCAdminTool.Commands
{
    public class AddRegionCacheName : AsyncCommandBindingBase
    {
        public AddRegionCacheName()
        {
            Command = Application.Current.Resources["AddRegionCacheName"] as ICommand;
        }

        protected override void OnExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            var cache = e.Parameter as MdcCache;
            CacheRegion cr = null;

            if (cache == null)
            {
                cr = e.Parameter as CacheRegion;

                if (cr != null)
                    cache = cr.Parent as MdcCache;
            }

            if (cache != null)
            {
                AddRegionWin win = null;
                bool isSave = false;
                string regionName = null;
                bool? evictionOn = null;

                Application.Current.Dispatcher.Invoke(new Action(() =>
                                                                 {
                                                                     win = new AddRegionWin();
                                                                     win.ShowDialog();
                                                                     isSave = win.IsSave;
                                                                     regionName = win.RegionName;
                                                                     evictionOn = win.EvictionOn;
                                                                     win.Close();
                                                                 }));
                if (isSave)
                {
                    cache.AddRegion(regionName, evictionOn ?? false);
                }
            }
        }

        protected override void OnCanExecute( object sender , CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = true;
        }
    }
}